package com.taxi.driver.data.order;

import com.taxi.driver.data.entity.CanGrapOrdersEntity;
import com.taxi.driver.data.entity.CancelDesEntity;
import com.taxi.driver.data.entity.ComplainResultEntity;
import com.taxi.driver.data.entity.OrderCostEntity;
import com.taxi.driver.data.entity.OrderEntity;
import com.taxi.driver.data.entity.OrderHomeStatusEntity;
import com.taxi.driver.data.entity.OrderSummaryEntity;
import com.taxi.driver.data.entity.WaitFare;
import com.taxi.driver.data.entity.WxpayInfo;
import com.taxi.driver.data.order.local.OrderLocalSource;
import com.taxi.driver.data.order.remote.OrderRemoteSource;

import java.util.List;

import javax.inject.Inject;

import rx.Observable;

/**
 * 功能描述：订单仓库
 */
public class OrderRepository implements OrderSource {

    private OrderLocalSource mLocalSource;
    private OrderRemoteSource mRemoteSource;

    @Inject
    public OrderRepository(OrderLocalSource localSource, OrderRemoteSource remoteSource) {
        mLocalSource = localSource;
        mRemoteSource = remoteSource;
    }

    @Override
    public void cacheOrder(OrderEntity entity) {
        //mLocalSource.cacheOrder(entity);
    }

    @Override
    public Observable<OrderHomeStatusEntity> reqHomeStatus() {
        return mRemoteSource.reqHomeStatus();
    }

    @Override
    public Observable<List<OrderSummaryEntity>> reqOrderList(int nowPage) {
        return mRemoteSource.reqOrderList(nowPage);
    }

    @Override
    public Observable<OrderEntity> reqGrab(String orderUuid) {
        return mRemoteSource.reqGrab(orderUuid);
    }

    @Override
    public Observable<OrderEntity> reqOrderDetail(String orderUuid) {
        return Observable.concat(mLocalSource.reqOrderDetail(orderUuid), //优先获取本地缓存的订单详情
                mRemoteSource.reqOrderDetail(orderUuid).doOnNext(mLocalSource::cacheOrder)) //请求订单详情，并缓存数据
                .takeFirst(entity -> entity != null); //返回不为空的第一项数据
    }

    @Override
    public Observable<OrderEntity> reqOrderDetail(String orderUuid, boolean onlyFromRemote) {
        if (onlyFromRemote) //只从服务端获取
            return mRemoteSource.reqOrderDetail(orderUuid).doOnNext(mLocalSource::cacheOrder);

        return reqOrderDetail(orderUuid);
    }

    @Override
    public Observable<String> reqPickUpPas(String orderUuid, String uploadText) {
        return mRemoteSource.reqPickUpPas(orderUuid, uploadText);
    }

    @Override
    public Observable<String> reqDepart(String orderUuid, String uploadText) {
        return mRemoteSource.reqDepart(orderUuid, uploadText);
    }

    @Override
    public Observable<String> reqGetOn(String orderUuid, double waitMile, String uploadText) {
        return mRemoteSource.reqGetOn(orderUuid, waitMile, uploadText);
    }

    @Override
    public Observable<String> reqArrive(String orderUuid, double tripDistance, String destAddress,
                                        String destDetailAddress, String destCity, double destLng,
                                        double destLat, String uploadText) {
        return mRemoteSource.reqArrive(orderUuid, tripDistance, destAddress,
                destDetailAddress, destCity, destLng,
                destLat, uploadText);
    }

    @Override
    public Observable<String> reqUpdateFare(String orderUuid, double actualFare) {
        return mRemoteSource.reqUpdateFare(orderUuid, actualFare);
    }

    @Override
    public Observable<String> receiveCash(String orderUuid, String uploadText) {
        return mRemoteSource.receiveCash(orderUuid, uploadText);
    }

    @Override
    public Observable<String> reqCancelOrder(String orderUuid, String cancelMsg, String uploadText) {
        return mRemoteSource.reqCancelOrder(orderUuid, cancelMsg, uploadText);
    }

    @Override
    public Observable<String> reqComplainOrder(String orderUuid, String contents, String remark) {
        return mRemoteSource.reqComplainOrder(orderUuid, contents, remark);
    }

    @Override
    public Observable<String> rushFare(String orderUuid) {
        return mRemoteSource.rushFare(orderUuid);
    }

    @Override
    public Observable<ComplainResultEntity> isComplain(String orderUuid) {
        return mRemoteSource.isComplain(orderUuid);
    }

    @Override
    public Observable<CancelDesEntity> cancelDescription(String orderUuid) {
        return mRemoteSource.cancelDescription(orderUuid);
    }

    @Override
    public Observable<WxpayInfo> payByWechat(String orderUuid, String spbillCreateIp) {
        return mRemoteSource.payByWechat(orderUuid, spbillCreateIp);
    }

    @Override
    public Observable<String> payByAlipay(String orderUuid) {
        return mRemoteSource.payByAlipay(orderUuid);
    }

    @Override
    public Observable<String> payByBalance(String orderUuid) {
        return mRemoteSource.payByBalance(orderUuid);
    }

    @Override
    public Observable<OrderCostEntity> reqFareItems(String orderUuid) {
        return mRemoteSource.reqFareItems(orderUuid);
    }

    @Override
    public Observable<OrderCostEntity> getRealtimeFare(String orderUuid) {
        return mRemoteSource.getRealtimeFare(orderUuid);
    }

    @Override
    public Observable<OrderCostEntity> orderFare(String orderUuid) {
        return mRemoteSource.orderFare(orderUuid);
    }

    @Override
    public Observable<String> confirmFare(String orderUuid, Integer highwayFare,
                                          Integer roadBridgeFare, Integer otherFare, String uploadText) {
        return mRemoteSource.confirmFare(orderUuid, highwayFare, roadBridgeFare, otherFare, uploadText);
    }

    @Override
    public Observable<WaitFare> waitFare(String orderUuid) {
        return mRemoteSource.waitFare(orderUuid);
    }

    @Override
    public Observable<List<OrderSummaryEntity>> getAppointmentList() {
        return mRemoteSource.getAppointmentList();
    }

    @Override
    public Observable<CanGrapOrdersEntity> canGrapOrders(String areaCode) {
        return mRemoteSource.canGrapOrders(areaCode);
    }

    @Override
    public Observable<List<OrderSummaryEntity>> getOrderPool(String areaCode) {
        return mRemoteSource.getOrderPool(areaCode);
    }

    @Override
    public void ignoringOrder(String orderUuid) {
        mLocalSource.ignoringOrder(orderUuid);
    }

    @Override
    public Observable<List<String>> ignoreList() {
        return mLocalSource.ignoreList();
    }
}
